package com.supply.dl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import com.supply.dl.R;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.BaseItemBean;
import com.supply.dl.bean.CheckDetailBean;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.CommonNormalAdapterUtil;
import com.supply.dl.utils.RecyclerViewUtil;
import com.supply.dl.widget.LoadTipFailedLayout;
import com.supply.dl.widget.adapter.CommonNormalAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 盘点明细
 */
public class CheckDetailsActivity extends BaseActivity
{
	@BindView(R.id.txt_depot_name) TextView txtDepotName;

	@BindView(R.id.txt_depot_code) TextView txtDepotCode;

	@BindView(R.id.txt_depot_lbs) TextView txtDepotLbs;

	@BindView(R.id.layout_load_failed) LoadTipFailedLayout loadTipFailedLayout;

	@BindView(R.id.recylerView_depot) RecyclerView recyclerView;
	CommonNormalAdapter commonNormalAdapter;
	List<BaseItemBean> itemList = new ArrayList<BaseItemBean>();

	public static final String CHECK_ID = "check_id";

	public String checkId;

	@Override protected int getLayoutResource()
	{
		return R.layout.activity_check_details;
	}

	@Override protected void initRecyclerView()
	{
		super.initRecyclerView();
		RecyclerViewUtil.initFullLinearLayoutVERTICAL(this, recyclerView);

		commonNormalAdapter = CommonNormalAdapterUtil.initCommonNormalAdapter(R.layout.item_check_detail, itemList, CommonNormalAdapter.FLAG_CHECK_DEATILS_MAIN);
		recyclerView.setAdapter(commonNormalAdapter);
	}

	@Override protected void onCreate(@Nullable Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setTitleShow(getResourcesString(R.string.check_details));
		showLeftBack();
		Intent intent = getIntent();
		if (intent != null)
		{
			checkId = intent.getStringExtra(CHECK_ID);
			initData(checkId);
		}
		loadTipFailedLayout.setRestartOnClickListener(this);
	}

	public void initData(String checkId)
	{
		loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_PROGRESS);
		HttpManger httpManger = HttpManger.getInstance();
		httpManger.setContext(this);
		httpManger.loadCheckDetails(checkId);
		httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener()
		{
			@Override public void onSuccess(BaseInfo<?> baseInfo)
			{

				List<CheckDetailBean> transGoodsBeanList = (List<CheckDetailBean>) baseInfo.getData();
				if (transGoodsBeanList.size() == 0)
					loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_NORMAL);
				else
					loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FINISH);
				itemList.clear();
				if (null != transGoodsBeanList && transGoodsBeanList.size() > 0)
				{
					for (CheckDetailBean locationBean : transGoodsBeanList)
					{
						itemList.add(new BaseItemBean(locationBean));
					}
				}
				if (transGoodsBeanList.size() > 0)
				{
					txtDepotName.setText("仓库名称：" + transGoodsBeanList.get(0).whName);
					txtDepotCode.setText("仓库编码：" + transGoodsBeanList.get(0).whCode);
					txtDepotLbs.setText("库位名称：" + transGoodsBeanList.get(0).stockName);
				}
				commonNormalAdapter.setNewData(itemList);
			}

			@Override public void onFailure(String message)
			{
				loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FAILED);
				showToast(message);
			}
		});

	}

	@Override public void onClick(View v)
	{
		super.onClick(v);
		switch (v.getId())
		{

		case R.id.txt_load_failed:

			initData(checkId);
			break;
		}
	}
}
